﻿using UnityEngine;
using System.Collections;
using System;

public class Countdown 
{
    public double duration;

    private int currentNumber;
    public int maxNumber;

    public DateTime lastBeginTime;

    public Countdown(double duration,long lastTime,int maxNumber,int currentNumber)
    {
        this.duration = duration;
        this.maxNumber = maxNumber;
        this.currentNumber = currentNumber;
        lastBeginTime = ServerTimerTool.Java2CSTime(lastTime);
		//Debug.Log("last begin Time: " + lastBeginTime);
    }

    public void ResetData(double duration, double initSurplusTime, int maxNumber, int currentNumber)
    {
        this.duration = duration;
        //this.surplusTime = initSurplusTime;
        this.maxNumber = maxNumber;
        this.currentNumber = currentNumber;
    }

    /// <summary>
    /// 距离下一次Count的时间(秒)
    /// </summary>
    /// <returns></returns>
    public double GetNextRecoveryTime() {
        int inc;
        this.RefheshSurplusTime(out inc);
        if (currentNumber >= maxNumber)
        {
            return 0;
        }
        else {
            return duration - (ServerTimerTool.CurrentUTCTime - lastBeginTime).TotalSeconds;
        }
    }

    public double GetAllRecoveryTime() {
        int inc;
        this.RefheshSurplusTime(out inc);
        if (currentNumber >= maxNumber)
        {
            return 0;
        }
        else {
            return GetNextRecoveryTime() + (maxNumber - currentNumber - 1) * duration;
        }
    }

    public double RefheshSurplusTime(out int increment)
    {
        if (currentNumber >= maxNumber)
        {
            increment = 0;
            return 0f;
        }
        else
        {
            //转换为毫秒值
            long tick = (ServerTimerTool.CurrentUTCTime.Ticks - lastBeginTime.Ticks) / 10000;
            
            double second = (double)tick / 1000.0;
            if(second >= duration)
            {
                increment = (int)(second / duration);
                if(currentNumber + increment >= maxNumber)
                {
                    increment = maxNumber - currentNumber;
                    currentNumber = maxNumber;
                    return 0f;
                }
                else
                {
                    currentNumber += increment;
					lastBeginTime = lastBeginTime.AddSeconds(duration * increment);
                    return duration * (increment + 1) - second;
                }
            }
            else
            {
                increment = 0;
                return duration - second;
            }
        }
    }


    public int Current
    {
        get
        {
            int inc;
            RefheshSurplusTime(out inc);
            return currentNumber;
        }
        set
        {
            int inc;
            RefheshSurplusTime(out inc);
            if (value < maxNumber && currentNumber >= maxNumber)
            {
                lastBeginTime = ServerTimerTool.CurrentUTCTime;
            }
            currentNumber = value;
        }
    }

    public void ResetCurrentAndMax(int current,int max)
    {
		if (current < max && currentNumber >= max)
        {
            lastBeginTime = ServerTimerTool.CurrentUTCTime;
        }
        currentNumber = current;
        maxNumber = max;
    }
}
